﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using PMS.BLL;
using PMS.Entity;
using PMS;
using PMS.Resources;
using PMS.Win;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Views.Base;

namespace PMS.DanhMucChung
{
    public partial class ProductLookup : FormPopupBase
    {
        private readonly SanPhamBO service = new SanPhamBO();
        public SanPham ProductSelected;
        private bool CoChiTiepNhap = false;

        public ProductLookup()
        {
            InitializeComponent();
            InitStyle();
            InitData();
        }

        public ProductLookup(bool coNhapKho)
        {
            CoChiTiepNhap = coNhapKho;
            InitializeComponent();
            InitStyle();
            InitData();
        }
      
        void InitStyle()
        {
            grvProductList.OptionsBehavior.Editable = false;
            grvProductList.OptionsView.ShowGroupPanel = false;
            grvProductList.OptionsView.ShowIndicator = true;

            grvProductList.OptionsView.ShowAutoFilterRow = true;
            grvProductList.OptionsView.ShowFilterPanelMode = ShowFilterPanelMode.Never;
            grvProductList.OptionsView.ShowGroupedColumns = false;
            grvProductList.OptionsMenu.EnableColumnMenu = false;
            grvProductList.OptionsCustomization.AllowColumnMoving = false;
            grvProductList.OptionsCustomization.AllowGroup = false;
            grvProductList.OptionsFilter.AllowFilterEditor = false;

            this.grvProductList.OptionsSelection.MultiSelect = false;
            this.grvProductList.IndicatorWidth = Constants.INDICATOR_WIDTH;
            //gridbinding
            this.colCode.FieldName = SanPhamColumns.MaThuoc.ToString();
            this.colHoaChat.FieldName = SanPhamColumns.TenHoatChat.ToString();
            this.colBietDuoc.FieldName = SanPhamColumns.TenBietDuoc.ToString();
            this.colHamLuong.FieldName = SanPhamColumns.NongDoHamLuong.ToString();
            this.colQuyCach.FieldName = SanPhamColumns.QuyCach.ToString();
            this.colSoDK.FieldName = SanPhamColumns.SoDangKy.ToString();
            this.colXuatXu.FieldName = SanPhamColumns.XuatXu.ToString();
            this.colNhaSX.FieldName = SanPhamColumns.NhaSX.ToString();
            this.colNhaNK.FieldName = SanPhamColumns.NhaNK.ToString();
            this.colUnit.FieldName = SanPhamColumns.MaDonVi.ToString();
            this.colCategory.FieldName = SanPhamColumns.MaNH.ToString();

        }

        void InitData()
        {
            try
            {
                if (CoChiTiepNhap)
                {
                    this.grdProductList.DataSource = service.GetAllSanPhamCoNhapKho();
                }
                else
                {
                    this.grdProductList.DataSource = service.GetAllSanPham();
                }
            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex);
            }

        }

        private void CloseDialog(DialogResult result)
        {
            try
            {
                if (result == DialogResult.OK)
                {
                    this.ProductSelected = (SanPham)grvProductList.GetFocusedRow();
                }
                else
                {
                    this.ProductSelected = null;
                }

                this.DialogResult = result;
            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex);
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            CloseDialog(DialogResult.OK);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            CloseDialog(DialogResult.Cancel);
        }

        private void grvProductList_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if ((e.Info.IsRowIndicator) && (e.RowHandle >= 0))
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

        private void grvProductList_DoubleClick(object sender, EventArgs e)
        {
            GridView view = sender as GridView;
            GridHitInfo hi = view.CalcHitInfo(grdProductList.PointToClient(MousePosition));
            if (hi.InRow || hi.InRowCell)
            {
                if (ProductSelected != null)
                {
                    CloseDialog(DialogResult.OK);
                }
            }
        }

        private void grvProductList_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                btnOk_Click(sender, null);
            }
            else if (e.KeyCode == Keys.Escape)
            {
                btnCancel_Click(sender, null);
            }
        }

        private void grvProductList_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            ProductSelected = this.grvProductList.GetFocusedRow() as SanPham;
        }

        private void grvProductList_ShowingEditor(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
        }
    }
}
